RStudio Tools and Tips

Greg Snow

April 21, 2020

RStudio Tools and Tips

(Mis)Quote



  • Experience is learning from your mistakes

  • Wisdom is learning from other peoples mistakes

Q & A



Q:
Why don’t you use tool/tip here?
A:
Because you have not taught me how to use tool/tip yet.

Keyboard Shortcuts

Tools > Keyboard Shortcuts Help

Tools > Keyboard Shortcuts Help

Tools > Keyboard Shortcuts Help

Tools > Keyboard Shortcuts Help



Alt-Shift-K also works (Option-shift-K on Mac)

Section Headers

Section Headers



Any line that starts with at least one “#” (making it a comment) and ends with at least 4 “=”, “-”, or “#” Becomes a Section Header

Section Headers



## I am a Section Header --------------

# So am I ====

##### Me Too! #####

Section Headers



Section Headers are used for:

  • Outline Panel
  • Jump To Section
  • Code Folding

(details on future slides)

Outline Panel

Outline Panel

Outline Panel

Outline Panel

Jump To Section

Jump To Section

Last/Next Location

Last/Next Location

Last/Next Location



Back and Next arrows will move within a file and between open tabs in the editor.

Code Folding

Code Folding



Code folding works on:

  • Sections (based on Section Headers)
  • Function Definitions
  • “if” function clauses
  • Loops (for, while, repeat)
  • Sections and Code blocks in .Rmd files
  • Possibly others


Look for triangles on the left, next to the row number.

Code Folding

Code Folding

Code Folding

Code Folding

Code Folding

Code Folding

Code Folding

Code Folding

Code Folding



You can also fold an arbitrary section (even just a section of a really long line) by highlighting the section then choosing Edit > Folding > Collapse.


Using the menu you can also “Fold All” or “Expand All”.

File Types

File Types

File Types

File Types

File Types

File Types

File Types

Rerun block of code

Rerun block of code



In R scripts we often run a block of code, make a few edits, then rerun that same block.


There is a button that makes this easier without having to re-select the block each time. It does not have any words, but is between the “Run” and “Source” buttons to the top right of the editor window.

Rerun block of code

Rerun block of code

Rerun block of code

Jump to Function Help/Definition

Jump to Function Help/Definition

  • Ctrl-. Brings up a dialog box for you to enter the name of a function.
    • It will show matching functions from files in the same directory/package.
    • Selecting a function will jump to that definition (opening the file if needed).
  • F1 while on a function name will open the help page
  • F2 while on a function name will jump to/open the source code like above.
    • For built in R functions a new read-only editor window will open with additional information.

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Jump to Function Help/Definition

Pop-out panes

Pop-out panes



If you want to copy between editing panes, or just compare 2 files side by side, you can pop out a pane.

Pop-out panes

Pop-out panes

Pop-out panes


  • You can pop out as many panes as you want.
  • Once one pane is popped out, you can drag tabs from the main RStudio window to a popped out pane.
  • On Windows, pressing the windows button and left or right arrow key will move the current window to take up half the screen. Pressing again will move it to another position (nice for 2 monitors)

Rename in Scope

Rename in Scope

Rename in Scope

Rename in Scope

Rename in Scope

Rectangular Select

Rectangular Select



Hold down the Alt key while selecting a region to make the selection rectangular for editing.

Rectangular Select

Rectangular Select

Rectangular Select

Rectangular Select

Rectangular Select

Rectangular Select

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Extract Variable/Function

Expand Selection

Expand Selection


  • Ctrl-Shift-Up Arrow will select the current scope.
  • Additional presses will expand the selection to the enclosing scope.
  • Ctrl-Shift-Down Arrow will shrink the selection back down one step.
  • Easy to see matching braces/brackets/parentheses/quotes/…

Expand Selection

Expand Selection

Expand Selection

Expand Selection

Find in Files

Find in Files


Common Problem (for me):
I remember I used this function once, but which project? which script file? I would like to see what I did to copy it.
Solution:
Find in Files (under the Edit Menu)

Find in Files

Find in Files

Find in Files

Find in Files

Find in Files

Command History from the Console

Command History from the Console


  • Up arrow in the console will scroll through recent commands
  • Start typing, then Ctrl-Up arrow will show recent commands starting with what was already typed
  • Use the history pane for more flexible searching

Command History

Command History

Command History

Command History

Command History

Command History

Other Topics

Other Topics


  • RStudio Projects
  • files pane
  • custom code snippets
  • R Notebooks
  • debugging
  • examining differences (diffr and diffobj packages)
  • terminal pane
  • database connections